using DA.AnimGraph;
using System;
using UnityEngine;
using UnityEngine.UIElements;

namespace DA.AnimGraph.Editor {

    internal class BlackboardEntryField : VisualElement {

        internal class InspectorData : InspectorDataSource {
            public AUID uuid;
            [InspectorField] public RefField<string> name = new();
            [InspectorField] public RefField<AGABlackboardType> type = new();
        }

        public event Action<AUID> OnDeleteSelf;
        //public event Action<InspectorDataSource, Action> OnClick;
        public event Action<AGA_Blackboard.Entry> OnValueChange;

        InspectorData data;

        Label variableName;
        Label typeName;
        InlineButton deleteButton;
        private readonly AGEContext ageContext;

        public BlackboardEntryField(AGEContext ageContext) {
            this.ageContext = ageContext;
            data = new InspectorData();

            OnDrawUI();
        }

        public void SetData(AGA_Blackboard.Entry entry) {
            data.uuid = entry.uuid;
            data.name.value = entry.name;
            data.type.value = entry.type;
            RefreshByData();
        }

        void InspectorSaveCallback() {
            RefreshByData();
            var newEntry = new AGA_Blackboard.Entry() {
                uuid = data.uuid,
                name = data.name.value,
                type = data.type.value,
            };
            OnValueChange?.Invoke(newEntry);
        }

        void RefreshByData() {
            variableName.text = data.name.value;
            typeName.text = data.type.value.ToString();
        }

        void OnDrawUI() {
            //横向排布
            style.flexDirection = FlexDirection.Row;
            variableName = new() {
                name = "name",
                style =
                {
                    unityTextAlign = TextAnchor.MiddleLeft,
                    marginTop = 1,
                    marginBottom = 1,
                    borderTopWidth = 1,
                    borderBottomWidth = 1,
                    paddingTop = 1,
                    paddingBottom = 1,
                    flexGrow = 1,
                }
            };
            variableName.RegisterCallback<MouseDownEvent>(ClickCallback);

            typeName = new() {
                name = "type",
                style =
                {
                    unityTextAlign = TextAnchor.MiddleRight,
                    marginTop = 1,
                    marginBottom = 1,
                    borderTopWidth = 1,
                    borderBottomWidth = 1,
                    paddingTop = 1,
                    paddingBottom = 1,
                    flexGrow = 1,
                }
            };
            typeName.RegisterCallback<MouseDownEvent>(ClickCallback);

            deleteButton = new("X");
            //删除按钮，删除自己
            deleteButton.clickable = new Clickable((evt) => {
                OnDeleteSelf?.Invoke(data.uuid);
            });

            Add(variableName);
            Add(typeName);
            Add(deleteButton);
        }

        private void ClickCallback(MouseDownEvent evt) {
            if (evt.clickCount == 1 && evt.button == 0) {
                var inspector = ageContext.serviceManager.GetService<ShowInspectorService>();
                if (!inspector.IsSourceEqual(data)) {
                    inspector.ShowInspector(data, InspectorSaveCallback, InspectorPane.ShowInspectorType.New);
                }
            }
        }
    }
}
